package com.hqyj.emb.system.controller;

import com.hqyj.emb.system.model.Role;
import com.hqyj.emb.system.model.User;
import com.hqyj.emb.system.model.UserRole;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import frame.controller.BaseController2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

@WebServlet("/roleController.do")
public class RoleController extends BaseController2 {

    public String list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Role> userList_db =roleService.queryAllUserRole();
        System.out.println(userList_db);
        req.setAttribute("userList_db",userList_db);
        return "view/system/role/roleList.jsp";
    }
    public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String state =req.getParameter("state");
        Role role=new Role();
        role.setName(name);
        role.setState(state);
        int n =roleService.addUserRole(role);
        List<Role> userList_db = roleService.queryAllUserRole();
        req.setAttribute("userList_db",userList_db);
        return "view/system/role/roleList.jsp";
    }
    public String delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleIdSTR = req.getParameter("roleIdSTR");
        int roleId=Integer.parseInt(roleIdSTR);

        int n =roleService.deleteRoleByUserId(roleId);
        List<Role> userList_db = roleService.queryAllUserRole();
        req.setAttribute("userList_db",userList_db);
        return "view/system/role/roleList.jsp";
    }
    public String updateUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleIdSTR = req.getParameter("roleIdSTR");
        int roleId=Integer.parseInt(roleIdSTR);

        Role role_db=roleService.queryUserRole(roleId);
        req.setAttribute("role_db",role_db);
        return "view/system/role/updateRole.jsp";
    }
    public String update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleIdSTR = req.getParameter("roleIdSTR");
        int roleId=Integer.parseInt(roleIdSTR);
        String name = req.getParameter("name");
        String state = req.getParameter("state");
        Role role=new Role();
        role.setRoleId(roleId);
        role.setName(name);
        role.setState(state);
        int n=roleService.updateUserRole(role);
        List<Role> userList_db = roleService.queryAllUserRole();
        req.setAttribute("userList_db",userList_db);
        return "view/system/role/roleList.jsp";
    }
    public String FPRoleUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userIdSTR = req.getParameter("userIdSTR");
        int userId=Integer.parseInt(userIdSTR);
        //用户选中id
        User user_db = userService.queryUserByUserId(userId);
        //所有角色
        List<Role> roles_db = roleService.queryAllUserRole();
        //选中的角色
        Role role_xz_db = roleService.queryUserRole(userId);
        req.setAttribute("user_db",user_db);
        req.setAttribute("roles_db",roles_db);
        req.setAttribute("role_xz_db",role_xz_db);
        return "view/system/role/FPRole.jsp";
    }
    public String FPRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userIdSTR = req.getParameter("userIdSTR");
        int userId=Integer.parseInt(userIdSTR);
        String roleIdSTR = req.getParameter("roleIdSTR");
        int roleId=Integer.parseInt(roleIdSTR);

        UserRole userRole=new UserRole();
        userRole.setRoleId(roleId);
        userRole.setUserId(userId);
        int i = userRoleService.deleteRoleByUserId(userId);
        int n = userRoleService.addUserRoleByUserId(userRole);


        //用户选中id
        User user_db = userService.queryUserByUserId(userId);
        //所有角色
        List<Role> roles_db = roleService.queryAllUserRole();
        req.setAttribute("message", "分配角色成功！继续分配吗？");
        //选中的角色
        Role role_xz_db = roleService.queryUserRole(userId);
        System.out.println(role_xz_db+"---"+userId);
        req.setAttribute("user_db",user_db);
        req.setAttribute("roles_db",roles_db);
        req.setAttribute("role_xz_db",role_xz_db);
        return "view/system/role/FPRole.jsp";
    }
    public String PLDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String checkId = req.getParameter("checkId");
        System.out.println(checkId);
        String[] split = checkId.split(",");
        int[] ids=new int[split.length];
        if(split.length>0){
            for(int i=0;i<split.length;i++){
                ids[i]=Integer.parseInt(split[i]);
            }
        }
        for (int id : ids) {
            int i =roleService.deleteRoleByUserId(id);
        }

        List<Role> userList_db = roleService.queryAllUserRole();
        req.setAttribute("userList_db",userList_db);
        return "view/system/role/roleList.jsp";
    }

//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String checkId = req.getParameter("checkId");
//        System.out.println(checkId);
//    }


    public void TJquery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> parameterMap = req.getParameterMap();
        Set<String> set = parameterMap.keySet();
        for (String s : set) {
            System.out.println(s+parameterMap.get(s ));
        }
        List<Role> roles = roleService.findTj(parameterMap);

    }
}

